function generateRandomArray (length) {
    const returnData = [];
    for(let i = 0; i < length; i++) {
        returnData.push(Math.floor(Math.random()*100));
    }
    return returnData;
}

function bubbleSort(array) {
    let temp = null;
    for(let i = 0; i < array.length; i++) {
        for(let j = i+1; j < array.length; j++) {
            if(array[i] > array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    };
    return array;
};

function selectionSort(array) {
    let temp = null;
    for(let i = 0; i < array.length - 2; i++) {
        let min = i;
        for(let j = i + 1; j < array.length -1; j++) {
            if(array[min] > array[j]) {
                min = j;
            }
        }
        temp = array[i];
        array[i] = array[min];
        array[min] = temp;
    }
    return array;
};

function insertionSort(array) {
    let temp = null;
    for(let i = 1; i < array.length; i++) {
        temp = array[i];
        let inner = i;
        while(inner > 0 && array[inner] < array[inner - 1]){
            array[inner] = array[inner - 1];
            inner--;
        }
        array[inner] = temp;
    };
    return array;
};

function shellSort(array) {
    //添加了动态间隔序列的算法
    let h = 1;
    while(h > array.length / 3) {
        h = 3 * h + 1;
    }
    while(h >= 1) {
        for(let i = h; i < array.length; i++) {
            for(let j = i; j >= h && array[j] < array[j - h]; j = j - h) {
                const temp = array[j];
                array[j] = array[j-h];
                array[j-h] = temp;
            }
        }
        h = (h - 1) / 3;
    }
    return array;
};

const array = generateRandomArray(10);
console.log(bubbleSort(array));
console.log(selectionSort(array));
console.log(insertionSort(array));
console.log(shellSort(array));